package cn.csust.net2.manager.client.module;

import java.util.List;

import cn.csust.net2.manager.client.ux.ExpandGrid;
import cn.csust.net2.manager.shared.po.PairCell;
import cn.csust.net2.manager.shared.po.Table;
import cn.csust.net2.manager.shared.service.ExamineInfoService;
import cn.csust.net2.manager.shared.service.ExamineInfoServiceAsync;
import cn.csust.net2.manager.shared.util.ClassForNameAble;
import cn.csust.net2.manager.shared.vo.TableVO;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout.HBoxLayoutAlign;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 申报审批
 * @author 韩忠金
 *
 */
@ClassForNameAble
public class ExamineInfo  extends BaseModule{
	ExamineInfoServiceAsync service;
	private LayoutContainer layout;
	public ExamineInfo(){
		super();
		service = GWT.create(ExamineInfoService.class);
		this.setText("申报选项");
		this.setId("申报选项");
		this.setClosable(true);
		layout = new LayoutContainer();
		layout.setLayout(new FitLayout());
		layout.setLayoutOnChange(true);
		layout.setScrollMode(Scroll.AUTO);
//		service.enterExamine("", new AsyncCallback<TableVO>() {
//			
//			@Override
//			public void onSuccess(TableVO result) {
//				// TODO Auto-generated method stub
//				init();
//			}
//			
//			@Override
//			public void onFailure(Throwable caught) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
		init();
		this.setLayout(new FitLayout());
		this.add(layout);
		this.addStyleName("pad-text");
		
	}
	 
	private void init(){
		
		
		ExpandGrid<Table> eg = new ExpandGrid<Table>(new Table(),service);
		eg.addListener(Events.OnDoubleClick, new Listener<GridEvent<BeanModel>>(){
			
			@Override
			public void handleEvent(GridEvent<BeanModel> be) {
				Table table =be.getModel().getBean();
				service.loadExamine(table, new AsyncCallback<TableVO>() {
					
					@Override
					public void onSuccess(TableVO result) {
						
						final Window window = new Window();
						window.setSize(800, 500);
						window.setLayout(new FitLayout());
						LayoutContainer [] con = new LayoutContainer [result.getTable().getRowCount()];
						for(int i=0;i<result.getTable().getRowCount();i++){
							
							LayoutContainer left = new LayoutContainer();  
							left.setStyleAttribute("padding-left", "140px");
							left.setStyleAttribute("padding-top", "10px");
							left.setAutoHeight(true);
							HBoxLayout hb = new HBoxLayout();
							hb.setHBoxLayoutAlign(HBoxLayoutAlign.TOP);
						    left.setLayout(hb);  
						  
						    con[i] = left;
						
						}
						 RowLayout vb = new RowLayout(Orientation.VERTICAL);
						 LayoutContainer  main = new LayoutContainer();  
						 main.setScrollMode(Scroll.AUTO);
						 main.setWidth(700);
						 main.setStyleAttribute("padding-top", "5px");  
						 main.setLayout(vb);
						 List<PairCell> pc = result.getList();
						for(PairCell p :pc){
							
							TextArea email = new TextArea();  
							email.setBorders(true);
							Label lable = new Label();
							lable.setWidth(p.getKey().getWidth());
							lable.setHeight(p.getKey().getHeight());
							lable.setText(p.getKey().getContent());
							email.setHeight(p.getValue().getHeight());
							email.setWidth(p.getValue().getWidth());
							email.setEmptyText(p.getValue().getContent());			
							con[p.getRow()-1].add(lable);
							con[p.getRow()-1].add(email);
						}
						  
						for(int i=0;i<result.getTable().getRowCount();i++){
							con[i].setStyleAttribute("padding-top", "10px");
							main.add(con[i]);
						}
						window.add(main);
						window.show();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						
						
					}
				});
			}

			
			 
		});
		
		ContentPanel	cp = new ContentPanel();
		cp.setWidth("100%");
		cp.setHeight("80%");
		cp.setHeaderVisible(false);
		cp.setLayout(new FitLayout());
		cp.add(eg);
		cp.setBottomComponent(eg.getToolBar());
		layout.add(cp);
		
	}
	
}
